<?php

/**
 * Server_Api_Collect
 * 收藏服务
 * Author: netman
 * DateTime:  2023/9/11 16:37
 */
class Service_Api_Collect extends Contract_Api_Collect
{

    use Trait_Api_Common;
    use Trait_Api_Image;

    public function __construct()
    {

    }

    /**
     * 收藏/取消操作
     * @param $typeid
     * @param $mid
     * @param $product_id
     * @return string[]
     * @throws Kohana_Exception
     * User: netman
     * Date: 2023/9/15 11:17
     */
    function modify($typeid, $mid, $product_id): array
    {
        $result = array(
            'msg' => ''
        );
        $collect =  ORM::factory('product_collect')->where('member_id','=', $mid)
            ->and_where('type_id','=',$typeid)
            ->and_where('auto_id', '=', $product_id)
            ->find();

        if ($collect->loaded()) {
            $collect->delete();
            if (!$collect->loaded()) {
                $result['msg']  = "取消收藏成功";
                $result['data']['is_collect'] = 0;
            }

        } else {

            $collect = ORM::factory('product_collect');
            $collect->type_id = $typeid;
            $collect->auto_id = $product_id;
            $collect->member_id = $mid;
            $collect->save();
            if ($collect->saved()) {
                $result['msg']  = "收藏成功";
                $result['data']['is_collect'] = 1;
            }
        }
        return $result;
    }

    /**
     * 获取收藏列表
     * @param $mid
     * @param $page
     * @param $pagesize
     * @return array
     * User: netman
     * Date: 2023/9/15 17:20
     */
    function get_collect_list($mid, $page, $pagesize): array
    {
        $models = ORM::factory('model')->where('isopen','=',1)->get_all();
        $model_ids = [];
        $model_arr = [];
        foreach ($models as $model) {
            array_push($model_ids, $model['id']);

            $model_arr[$model['id']] = $model;
        }

        //列表
        $offset = $page ?  ($page - 1) * $pagesize : 0;
        $list = DB::select()->from('product_collect')
            ->where('member_id', '=', $mid)
            ->and_where('type_id','in', $model_ids)
            ->offset($offset)
            ->order_by("id", "DESC")
            ->limit($pagesize)
            ->execute()
            ->as_array();
        $out = [];
        foreach ($list as $k=>&$item) {
            $db = $model_arr[$item['type_id']]['maintable'];
            $d = DB::select()->from($db)->where('id', '=', $item['auto_id'])->execute()->current();
            //检查是否已经删除
            if(!$d){
                unset($list[$k]);
                continue;
            }
            if ($db != 'guide_service'  &&  $db !="combination") {
                $_model           = "Model_" . ucfirst($db);
                $reflectionMethod = new ReflectionMethod($_model, 'get_minprice');
                $item['price']    = $reflectionMethod->invoke(new $_model(), $d['id'], array('info' => $d));
            } else {
                $item['price'] = $d['price'];
            }
            //门票的get_minprice返回不一样
            if ($item['type_id'] == 5) {
                $item['price'] = $item['price']['price'];
            }

            // 获取详情页面地址
            $item['commentnum']   = Model_Comment::get_comment_num($item['auto_id'], $item['type_id']);
            $item['satisfyscore'] = St_Functions::get_satisfy($item['type_id'], $d['id'], $d['satisfyscore']);
            $item['title']        = $d['title'];
            $item['litpic']       = $this->img_url($d['litpic'],750,400);
            $item['model']        = $model_arr[$item['type_id']]['modulename'];
            $out[] = $item;
        }

        //是否最后一页
        $result = [
            'list' => $out,
            'completed' => count($out) < $pagesize ? 1 : 0
        ];
        return $result;
    }

    /**
     * 获取收藏数量
     * @param $mid
     * @return int
     * User: netman
     * Date: 2023/9/18 9:50
     */
    function get_collect_num($mid): int
    {
        $num  = DB::select(DB::expr("count(id) as num"))->from('product_collect')
            ->where('member_id', '=', $mid)
            ->execute()
            ->get("num");
        return $num;
    }

    /**
     * 删除收藏
     * @param string $collect_ids
     * @param $mid
     * @return bool
     * User: netman
     * Date: 2023/9/18 9:58
     */
    function del_collect_product(string $collect_ids, $mid): bool
    {
        // 删除收藏
        $id_arr = explode(',', $collect_ids);
        $flag = DB::delete('product_collect')->where('member_id', '=', $mid)
            ->and_where('id', "in", $id_arr)
            ->execute();
        return (bool)$flag;
    }

    function is_collect($typeid, $mid, $product_id): bool
    {
        //产品收藏情况
       $collect = ORM::factory('product_collect')->where('member_id','=', $mid)
            ->and_where('type_id','=',$typeid)
            ->and_where('auto_id', '=', $product_id)
            ->count_all();
       if ($collect) {
           return true;
       } else {
           return false;
       }
    }


}